import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.objects.NativeUint8Array;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

/**
 * Created by lkkjzx on 2017/4/25.
 */
public class MysqlPipeline implements Pipeline {
    @Override
    public void process(ResultItems resultItems, Task task) {

        String resource = "mybatisConfig.xml";
        Reader reader = null;
        SqlSession session = null;
        SqlSessionFactory ssf = null;
        try {


            Party partyInfo = resultItems.get("partyInfo");
            if (partyInfo == null) return;

            reader = Resources.getResourceAsReader(resource);
            ssf = new SqlSessionFactoryBuilder().build(reader);
            session = ssf.openSession();

//            System.out.println(partyInfo);

            String partyId = partyInfo.getParty_id();
            //添加party信息
            Party dataPartyInfo = session.selectOne("com.twitso.interactSpider.Party.select", partyInfo.getParty_id());
            if (dataPartyInfo == null) {
                session.insert("com.twitso.interactSpider.Party.insert", partyInfo);
            }
            JSONArray joinUserInfo = resultItems.get("joinUserInfo");
            if (joinUserInfo != null && joinUserInfo.size() > 0) {
                for (int i = 0; i < joinUserInfo.size(); i++) {
                    JSONObject userObj = joinUserInfo.getJSONObject(i);

                    User user = JSONObject.toJavaObject(userObj, User.class);
                    if (user == null) continue;
                    //添加用户信息
                    User dataUser = session.selectOne("com.twitso.interactSpider.User.selectUser", user.getUserId());
                    if (dataUser == null) {
                        session.insert("com.twitso.interactSpider.User.insertUser", user);
                    } else {
                        session.update("com.twitso.interactSpider.User.updateUserFromJoin", user);
                    }
                    //添加参加用户和活动的对应关系
                    UserPartyMapper userPartyMapper = JSONObject.toJavaObject(userObj, UserPartyMapper.class);
                    if (userPartyMapper.getJoin_id() != 0) {
                        UserPartyMapper dataUserPartyMapper = session.selectOne("com.twitso.interactSpider.userPartyMapper.select", userPartyMapper.getJoin_id());
                        if (dataUserPartyMapper == null) {
                            userPartyMapper.setParty_id(partyId);
                            session.insert("com.twitso.interactSpider.userPartyMapper.insert", userPartyMapper);
                        }
                    }
                }
            }
            JSONObject authorInfo = resultItems.get("authorInfo");
            if (authorInfo != null) {
                User author = JSONObject.toJavaObject(authorInfo, User.class);
                //更新user信息
                User dataUser = session.selectOne("com.twitso.interactSpider.User.selectUser", author.getUserId());
                if (dataUser == null) {
                    session.insert("com.twitso.interactSpider.User.insertUser", author);
                } else {
                    session.update("com.twitso.interactSpider.User.updateUserFromAuthor", author);
                }
            }
            session.commit();
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            if (session != null) {
                session.close();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
